#include "XSystem/XCore.h"

// Constuct the label with the pre-defined data.
XLabel::XLabel(XResourcesManager *data, int positionX, int positionY, std::string text) : XWidget(data, 0, 0, positionX, positionY)
{
	x_type = XG_TYP_LABEL;

	x_string.SetFont(x_data->GetFont());
	x_string.SetSize(8);
	x_string.SetPosition(x_positionX, x_positionY);
	x_string.SetColor(sf::Color(0, 0, 0));
	x_string.SetText(text);

	x_text = text;
	x_align = 0;

	x_strings.SetFont(x_data->GetFont());
	x_strings.SetSize(8);
	x_strings.SetPosition(x_positionX+1, x_positionY+1);
	x_strings.SetColor(sf::Color(0, 0, 0));
	x_strings.SetText(text);

	x_background = false;
	x_shadow = false; 

	if (text.size() > 0)
	{
		sf::FloatRect t_labelRect = x_string.GetRect();
		int x_height = t_labelRect.GetHeight();
		int x_width = t_labelRect.GetWidth();

		x_backgroundShape = sf::Shape::Rectangle(-2+x_positionX, -2+x_positionY, x_positionX+x_width+2, x_positionY+x_height+2, sf::Color(0, 0, 0, 128));
	}
}

// Draws the label.
void XLabel::Draw()
{
	if (x_draw)
	{
		if (x_text.size() > 0)
		{
			if (x_background)
				x_app->Draw(x_backgroundShape);

			if (x_shadow)
				x_app->Draw(x_strings);

			x_app->Draw(x_string);
		}
	}
}

// Check the state of the label.
void XLabel::CheckState(const sf::Input &input)
{
	XWidget::CheckState(input);

	switch (x_state)
	{
		case XG_ST_MOUSE_ENTER:
			break;

		case XG_ST_MOUSE_EXIT:
			break;

		case XG_ST_LBUTTON_DOWN:
			break;

		case XG_ST_LBUTTON_RELEASED:
			break;
	}
}

// Set the label text.
void XLabel::SetText(std::string text, bool isStatic)
{
	x_string.SetText(text);
	x_strings.SetText(text);
	x_text = text;

	sf::FloatRect t_labelRect = x_string.GetRect();
	x_height = t_labelRect.GetHeight();
    x_width = t_labelRect.GetWidth();

	if (isStatic)
	{
		x_string.SetPosition(x_startingPositionX, x_startingPositionY);
		x_strings.SetPosition(x_startingPositionX+1, x_startingPositionY+1);
		x_backgroundShape = sf::Shape::Rectangle(-2+x_startingPositionX, -2+x_startingPositionY, x_startingPositionX+x_width+2, x_startingPositionY+x_height+2, sf::Color(0, 0, 0, 128));

	} else {

		x_string.SetPosition(x_positionX, x_positionY);
		x_strings.SetPosition(x_positionX+1, x_positionY+1);
		x_backgroundShape = sf::Shape::Rectangle(-2+x_positionX, -2+x_positionY, x_positionX+x_width+2, x_positionY+x_height+2, sf::Color(0, 0, 0, 128));
	}

	if (x_align == 1)
	{
		x_backgroundShape.Move(-(x_width/2), 0);
		x_string.Move(-(x_width/2), 0);
		x_strings.Move(-(x_width/2), 0);
	}
}

// \return the text.
std::string XLabel::GetText()
{
	return x_text;
}

// Set the background of the label.
void XLabel::SetBackground(bool isBackground)
{
	x_background = isBackground;
}

// Set the color of the label.
void XLabel::SetColor(sf::Color color)
{
	x_string.SetColor(color);
}

// Set if the text has a black shadow or not
void XLabel::SetShadow(bool isShadow)
{
	x_shadow = isShadow;
}

// Set the alignement of the label
void XLabel::SetAlign(std::string align)
{
	if (align == "left")
	{
		x_align = 0;
	}

	if (align == "center")
	{
		x_align = 1;
		x_backgroundShape.Move(-x_width/2, 0);
		x_string.Move(-x_width/2, 0);
		x_strings.Move(-x_width/2, 0);
	}

	if (align == "right")
	{
		x_align = 2;
	}
}

// Set the position of the label
void XLabel::SetPosition(int positionX, int positionY)
{
	XWidget::SetPosition(positionX, positionY);

	x_backgroundShape.SetPosition(positionX, positionY);
	x_string.SetPosition(positionX, positionY);
	x_strings.SetPosition(positionX+1, positionY+1);

	if (x_align == 1)
	{
		x_backgroundShape.Move(-(x_width/2), 0);
		x_string.Move(-(x_width/2), 0);
		x_strings.Move(-(x_width/2), 0);
	}
}
